// 传入的时间后的所有以30分钟为基数的时间的+-15范围的时间段(16:45~17:15)组成数组
function funGetTimeRange30(time) {
	const timeArray = [];
	// const now = new Date();
	// 计算当前时间距离下一个 30 分钟间隔的偏移量
	const minutes = time.getMinutes();
	let next30MinuteOffset;
	if (minutes < 30) {
		next30MinuteOffset = 30 - minutes;
	} else {
		next30MinuteOffset = 60 - minutes;
	}
	// 获取下一个 30 分钟间隔的时间
	const nextTime = new Date(time.getTime() + next30MinuteOffset * 60 * 1000);

	// console.log(time.getTime());
	// 不包含当前时间最近的一个时间，所以从下一个 30 分钟间隔开始
	let currentTime = new Date(nextTime.getTime() + 30 * 60 * 1000);
	// 获取当天 23:59:59 的时间
	const endOfDay = new Date(time.getFullYear(), time.getMonth(), time.getDate(), 21, 1, 1);

	while (currentTime <= endOfDay) {
		const frontTime = funChange15Min(currentTime, '-');
		const behindTime = funChange15Min(currentTime, '+');
		const hours1 = String(frontTime.getHours()).padStart(2, '0');
		const mins1 = String(frontTime.getMinutes()).padStart(2, '0');
		const hours2 = String(behindTime.getHours()).padStart(2, '0');
		const mins2 = String(behindTime.getMinutes()).padStart(2, '0');
		const timeString = `${hours1}:${mins1}~${hours2}:${mins2}`
		timeArray.push(timeString);
		currentTime = new Date(currentTime.getTime() + 30 * 60 * 1000)
	}

	return timeArray;
}


// 通过传递时间(非时间戳)，运算符号，获取该时间的15分钟前/后时间
const funChange15Min = (time, sign) => {
	switch (sign) {
		case '+':
			return new Date(time.getTime() + 15 * 60 * 1000);
		case '-':
			return new Date(time.getTime() - 15 * 60 * 1000);
		default:
			console.error('不支持的运算符');
			return null;
	}
};

export { funGetTimeRange30, funChange15Min }